﻿using DiverseStudents.UI;
using UnityEngine;
using UnityEngine.UI;

namespace DiverseStudents.Mods
{
    internal class ModsUIDescribe
    {
        public ScrollRect desScroll;
        public SpriteRenderer desImg;
        public Text desText;
        public Button desPath;
        public HorizontalLayoutGroup desTags;
        public VerticalLayoutGroup desConflicts;
        private RectTransform desScrollRect;
        private void Create()
        {
            desScroll = UICreate.ScrollRect("Right");
            if (desScroll == null)
            {
                return;
            }
            desScrollRect = desScroll.GetComponent<RectTransform>();
            GameObject img = new GameObject("Mod_desImg", typeof(RectTransform), typeof(SpriteRenderer));
            desImg = img.GetComponent<SpriteRenderer>();
            img.transform.SetParent(desScrollRect, false);
            GameObject txt = new GameObject("Mod_desText", typeof(Text));
            desText = txt.GetComponent<Text>();
            txt.transform.SetParent(desScrollRect, false);
            GameObject path = new GameObject("Mod_desPath", typeof(Button));
            desPath = path.GetComponent<Button>();
            path.transform.SetParent(desScrollRect, false);
            GameObject tags = new GameObject("Mod_desTags", typeof(HorizontalLayoutGroup));
            desTags = tags.GetComponent<HorizontalLayoutGroup>();
            tags.transform.SetParent(desScrollRect, false);
            GameObject conf = new GameObject("Mod_desConflicts", typeof(VerticalLayoutGroup));
            desConflicts = conf.GetComponent<VerticalLayoutGroup>();
            conf.transform.SetParent(desScrollRect, false);
        }

        public void UpdateRect(RectTransform baseRect)
        {
            if (baseRect == null)
            {
                return;
            }
            Create();
            if (desScrollRect != null)
            {
                Vector2 center = new Vector2(0.5f, 0.5f);
                desScrollRect.anchorMax = center;
                desScrollRect.anchorMin = center;
                desScrollRect.sizeDelta = new Vector2(baseRect.rect.width*0.26f, baseRect.rect.height * 0.84f);
                desScrollRect.anchoredPosition += new Vector2(baseRect.rect.width *0.2f+ baseRect.rect.width *0.02f, -baseRect.rect.height * 0.02f);
                var imgrec = desImg.GetComponent<RectTransform>();
                if (imgrec != null)
                {
                    imgrec.sizeDelta = new Vector2(desScrollRect.rect.width * 0.94f, desScrollRect.rect.width * 0.52f);
                    imgrec.anchoredPosition += new Vector2(0, desScrollRect.rect.height / 2 - imgrec.rect.height / 2 - desScrollRect.rect.width * 0.03f);
                }
                desScrollRect.SetParent(baseRect, false);
               // desScroll.transform.SetParent(baseRect, false);
            }

        }

        public void Destroy()
        {
            if (desScroll != null)
            {
                GameObject.Destroy(desScroll.gameObject);
            }
        }
    }
}
